package com.tohours.aia.web.action;

import java.io.IOException;

import lombok.Data;
import lombok.EqualsAndHashCode;
import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.util.Assert;

import com.tohours.aia.constants.Constants;
import com.tohours.aia.entity.Customer;
import com.tohours.aia.exception.BusinessException;

@EqualsAndHashCode(callSuper = false)
@Namespace("/")
public @Data class LoginAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String idOrSplit;
	
	@Action("index")
	public String index() throws IOException{
		return getOsType() + "_login_pre";
	}
	
	@Action("login")
	public void login(){
		Assert.notNull(idOrSplit);
		JSONObject json = new JSONObject();
		try{
			Customer customer = super.getAwardService().login(idOrSplit);
			super.getSession().setAttribute(Constants.SESSION_USER_ID, customer.getId());
			super.getSession().setAttribute(Constants.SESSION_USER_NAME, customer.getCustomerName());
			json.put("success", true);
		} catch(BusinessException e){
			json.put("success", false);
			json.put("msg", e.getMessage());
		} finally {
			super.printToHtml(json.toString());
		}
	}
	
	@Action("logout")
	public void logout(){
		super.getSession().removeAttribute(Constants.SESSION_USER_NAME);
		super.getSession().removeAttribute(Constants.SESSION_USER_ID);
		super.jump("index.action");
	}
}
